#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;

use Getopt::Std;


our($opt_h,$opt_p,$opt_n);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
my $wgcmd = "/usr/bin/wgcmd";

getopts('h:p:i:d');

unless ($opt_h && $opt_p && $opt_i && $opt_d) {
    &usage;
    exit -1;
}

# do our work

my $args = "$wgcmd -u status -p '$opt_p' $opt_h status /network/counter/if_list | xpath -q -e \"/status/network/counter/if_list/if[ifname=\'$opt_i\']/${opt_d}_bytes/text()\"";

my $buf  = `$args`; 

if ($?>>8 > 0) {
    print "error: /system/tasks xpath failed\n";
    exit -1;
}

if ($buf =~ m/<vsz>(.*)<\/vsz>/) {
    my $cpu_percent = $1;
    $cpu_percent =~ s/^\s+//;
    $cpu_percent =~ s/\s+$//;
    print "$cpu_percent";
} else {
    print "U";
}

sub usage {
    print "Usage: $0 -h <host> -p <ro pass> -i <ifname:eth0..> -d <send|recv>#>\n"; 
}
